/**
 * Created by Administrator on 2015/2/8 0008.
 */
var financeApp = angular.module("app.financeIndex",[]);
var incomeInfo,payInfo,userName,payId;
function delArray(obj,id){var copyObj = obj;
    for(var i =0 ;i<copyObj.length;i++){
        if(copyObj[i].id == id){obj.splice(i,1);}
    }
}
function getUserRoleName(){var roleName = $("#userRoleName").val();if(roleName == 'phoneAdmin' || roleName == 'admin') return true;else return false;}
financeApp.filter("dataFormat",function(){return function(input){return input.toString().substring(0,input.toString().lastIndexOf(":"));}});
financeApp.filter("imgCount",function(){return function(input){if(input == null)return true; else return false;}});
function setMoney(leftMoney,payMoney,yueCount){$("#leftMoney").html(leftMoney);$("#payMoney").html(payMoney);$("#yueCount").html(yueCount);}
//财务页面
financeApp.controller("financeIndex",['$scope','$http','ngDialog','$filter',function($scope,$http,ngDialog,$filter){
    $scope.userPerHide = getUserRoleName();
    $http.get('management/finance/findFinanceCount').success(function(data){
        userName = data.userName;$scope.leftMoney = data.financeCount.leftMoney;
        $scope.payMoney = data.financeCount.payMoney;$scope.yueCount = data.financeCount.zoneId;
    });
    $http.get('management/finance/findIncome?pageNum=1').success(function(data){
        incomeInfo = data.list;$scope.incomeInfo = incomeInfo;
        $scope.total = data.total;
        $scope.pageSize = data.pageSize;
        $scope.numPerPage = 1;
    });$scope.incomeQueryDate = false; var incomeStartTime,incomeEndTime;
    $scope.pageChange = function(page,bool){
        if(bool != null)$scope.incomeQueryDate = bool;
        if($scope.incomeQueryDate){incomeStartTime = $("#incomeStartTime").val();incomeEndTime = $("#incomeEndTime").val();}
        $http.get('management/finance/findIncome',{params: {pageNum:page,incomeStartTime:incomeStartTime,
            incomeEndTime:incomeEndTime}}).success(function(data){
            incomeInfo = data.list;$scope.incomeInfo = incomeInfo;
            $scope.total = data.total;
            $scope.pageSize = data.pageSize;
            $scope.numPerPage = 1;
        });
    };
    $http.get('management/finance/findPay?pageNum=1').success(function(data){
        payInfo = data.list;$scope.payInfo = payInfo;
        $scope.payTotal = data.total;
        $scope.payPageSize = data.pageSize;
        $scope.numPerPage = 1;
    });$scope.payQueryDate = false; var payStartTime,payEndTime;
    $scope.pageChangePay = function(page,bool){if(bool != null)$scope.payQueryDate = bool;
        if($scope.payQueryDate){payStartTime = $("#payStartTime").val();payEndTime = $("#payEndTime").val();}
        $http.get('management/finance/findPay',{params: {pageNum:page,payStartTime:payStartTime,
            payEndTime:payEndTime}}).success(function(data){
            payInfo = data.list;$scope.payInfo = payInfo;
            $scope.payTotal = data.total;
            $scope.payPageSize = data.pageSize;
            $scope.numPerPage = 1;
        });
    };
    $scope.del = function(id,money,left){
        $http.get("management/finance/del?id="+id+"&money="+money+"&left="+left).success(function(data){
            delArray(incomeInfo,id);delArray(payInfo,id);
            setMoney(data.leftMoney,data.payMoney,data.yueCount);
        });
    };
    $scope.openAddIncome = function(){
        ngDialog.open({
            template: 'finance/addIncome', className:'ngdialog-theme-flat',
            width:720, height:435
        });
    };
    $scope.openAddPay = function(){
        ngDialog.open({
            template: 'finance/addPay', className:'ngdialog-theme-flat',
            width:1100, height:650
        });
    };
    $scope.openImg = function(id){
        payId = id;ngDialog.open({
            template: 'finance/financeImg', className:'ngdialog-theme-flat',
            width:1100, height:650
        });
    };
    $scope.incomeOrder = function(rowName) {
        return $scope.row !== rowName ? ($scope.row = rowName,
        $scope.incomeInfo = $filter("orderBy")($scope.incomeInfo, rowName), $scope.onOrderChange()) : void 0
    };
    $scope.payOrder = function(rowName) {
        return $scope.row !== rowName ? ($scope.row = rowName,
        $scope.payInfo = $filter("orderBy")($scope.payInfo, rowName), $scope.onOrderChange()) : void 0
    };
}]);
//添加收入信息
financeApp.controller("addIncome",['$scope','$http','ngDialog','$timeout',function($scope,$http,ngDialog,$timeout){
    $scope.isHide = true;
    $scope.incomeForm = {};
    $scope.incomeInvalid = true;
    $scope.invalidMoney = function(){
        var reg = /^[0-9]+(\.[0-9]{1,2})?$/;
        var val = $scope.incomeForm.money;
        if(!reg.test(val)){$("#incomeMoney").attr('style',"border:1px solid red");$scope.incomeInvalid = true;}
        else {$("#incomeMoney").attr('style',"");$scope.incomeInvalid = false;}
    };
    $scope.addIncome = function(){
        $http({
            method  : 'post',
            url     : 'management/finance/addIncome',
            data    : $.param($scope.incomeForm),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            var date = new Date();var varDay=date.getDate();
            var month = (date.getMonth()+1)<10?"0"+(date.getMonth()+1):(date.getMonth()+1);
            var day = varDay<10?"0"+varDay:varDay;
            $scope.incomeForm.date = date.getFullYear()+"-"+month+"-"+day+" "+date.getHours()+":"+date.getMinutes()+":";
            $scope.incomeForm.addUId = userName;
            $scope.incomeForm.id=data.id;
            $scope.incomeForm.balance = data.yueCount;
            incomeInfo.push($scope.incomeForm);
            setMoney(data.leftMoney,data.payMoney,data.yueCount);
            ngDialog.close('finance/addIncome');
        }).error(function(data){
            $scope.isHide = false;
        });
        $timeout(function(){$scope.isHide = true;},5000);
    }
}]);
//添加支出
financeApp.controller("addPay",['$scope','$http','ngDialog','$timeout','FileUploader',function($scope,$http,ngDialog,$timeout,FileUploader){
    $scope.isHide = true;$scope.fileHide = true;$scope.payInvalid = true;
    $scope.payForm = {};
    $scope.showFileUpload = function(){if($('#ynShow').is(':checked')){$scope.fileHide = false;$("#paySubmit").attr("disabled",true);}
    else{$scope.fileHide = true;$("#paySubmit").attr("disabled",false);}};
    $scope.errorShow = "";
    $scope.addPay = function(){
        var reg = /^[0-9]+(\.[0-9]{1,2})?$/;
        var val = $scope.payForm.money;
        if(!reg.test(val)){$scope.isHide = false;$scope.errorShow="输 入 金 额 格 式 不 对";return;}
        $http({
            method  : 'post',
            url     : 'management/finance/addPay',
            data    : $.param($scope.payForm),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            var date = new Date();var varDay=date.getDate();
            var month = (date.getMonth()+1)<10?"0"+(date.getMonth()+1):(date.getMonth()+1);
            var day = varDay<10?"0"+varDay:varDay;
            $scope.payForm.date = date.getFullYear()+"-"+month+"-"+day+" "+date.getHours()+":"+date.getMinutes()+":";
            $scope.payForm.addUId = userName;
            $scope.payForm.id = data.id;
            $scope.payForm.balance = data.yueCount;
            payInfo.push($scope.payForm);
            setMoney(data.leftMoney,data.payMoney,data.yueCount);
            ngDialog.close('finance/addPay');
        }).error(function(data){
            $scope.isHide = false;
            $scope.errorShow = "添 加 失 败";
        });
        $timeout(function(){$scope.isHide = true;},5000);
    };
    $scope.setFileSubmit = function(){$("#paySubmit").attr("disabled",true);}
    var uploader = $scope.uploader = new FileUploader({
        url: 'management/finance/uploadBill'
    });
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        $("#paySubmit").attr("disabled",false);
    };
}]);
//票据查看
financeApp.controller("seeImg",['$scope','$http',function($scope,$http){
    $http.get('management/finance/findImg?id='+payId).success(function(data){
        if(data.length == 0){$scope.imgUrl = 'f6aefb6e7c786353ca9526c125da1f3b.jpg';$scope.date='暂无票据'}
        else{$scope.imgUrl = data[0].imgUrl;$scope.date = "上传时间"+data[0].date;$scope.img = [];
            for(var i = 1;i<data.length;i++){$scope.img.push(data[i]);}
        }
    });
}]);
